using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using SlimDX;
using SlimDX.Direct3D9;
using SlimDX.Windows;

namespace miciv
{
    public class XmlListenerLocalization : XmlListener
    {
        protected StringTableManager m_stringTableManager;
        protected StringTable m_stringTable;

        public XmlListenerLocalization(StringTableManager _stringTableManager)
        {
            this.m_stringTable = null;
            this.m_stringTableManager = _stringTableManager;
            Subscribe("fonttable", this.fonttableStartTagEventHandler, this.fonttableEndTagEventHandler);
            Subscribe("font", this.fontStartTagEventHandler, this.fontEndTagEventHandler);
            Subscribe("stringtable", this.stringtableStartTagEventHandler, this.stringtableEndTagEventHandler);
            Subscribe("string", this.stringStartTagEventHandler, this.stringEndTagEventHandler);
        }

        override public void Initialize(CoreContext _coreContext, XmlTraverser _xmlTraverser)
        {
            base.Initialize(_coreContext, _xmlTraverser);
        }

        protected bool fonttableStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            return true;
        }

        protected bool fonttableEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool fontStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            if (null == this.m_coreContext.GetData<BitmapFontManager>("BitmapFontManager").GetFont(_xmlAttributes["id"].Value))
            {
                BitmapFont font = new BitmapFont();
                if (false != font.Initialize(this.m_coreContext, _xmlAttributes["location"].Value))
                {
                    return this.m_coreContext.GetData<BitmapFontManager>("BitmapFontManager").AddFont(_xmlAttributes["id"].Value, font);
                }
            }
            return false;
        }

        protected bool fontEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool stringtableStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            if (null == this.m_stringTable)
            {
                this.m_stringTable = this.m_stringTableManager.GetStringTable(_xmlAttributes["lang"].Value);
                return (null != this.m_stringTable);
            }
            this.m_stringTable = null;
            return false;
        }

        protected bool stringtableEndTagEventHandler(string _strName)
        {
            this.m_stringTable = null;
            return true;
        }

        protected bool stringStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            if (null == this.m_stringTable.GetString(_xmlAttributes["id"].Value))
            {
                StringInfo stringInfo = new StringInfo(_xmlAttributes["text"].Value, _xmlAttributes["fontid"].Value);
                return this.m_stringTable.AddString(_xmlAttributes["id"].Value, stringInfo);
            }
            return false;
        }

        protected bool stringEndTagEventHandler(string _strName)
        {
            return true;
        }
    }
}
